package com.choudou5.webflux.web;

import com.choudou5.webflux.dto.DemoReq;
import com.choudou5.webflux.dto.DemoRsp;
import com.choudou5.webflux.service.DemoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.validation.Valid;


@RestController
@RequestMapping("/demo")
public class DemoController {

    private DemoService demoService;

    public DemoController(DemoService demoService) {
        this.demoService = demoService;
    }

    @GetMapping("/{type}/getIncrement")
    public Mono<DemoRsp> getIncrement(@PathVariable String type, @Valid DemoReq req) {
        Flux<Long> ids = demoService.getIncrement(type, req);
        return assembleResponse(ids);
    }

    private Mono<DemoRsp> assembleResponse(Flux<Long> ids) {
        return ids.collectList().map(list -> {
            DemoRsp result = new DemoRsp();
            result.setItemIDs(list);
            result.setSize(list.size());
            return result;
        });
    }

}
